/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-05-21     chenhy       the first version
 */
#include "proc_bar.h"
#include <stdio.h>
#include <string.h>
#define PROC_BAR_LENGTH 50

void bar_update(int cur_pos, int end_pos)
{
  int rate, pos;
  char bar[PROC_BAR_LENGTH+1] = {0};
  if(cur_pos > end_pos) rate = 100;
  else rate = cur_pos * 100 / end_pos;
  pos = rate * PROC_BAR_LENGTH / 100;
  if(pos < PROC_BAR_LENGTH) {
    memset(&bar[0], '=', pos % PROC_BAR_LENGTH);
    bar[pos % PROC_BAR_LENGTH] = '>';
    memset(&bar[(pos%PROC_BAR_LENGTH)+1], '-', PROC_BAR_LENGTH-(pos%PROC_BAR_LENGTH));
  }
  else {
    memset(&bar[0], '=', PROC_BAR_LENGTH);
  }
  bar[sizeof(bar)-1] = '\0';
  printf("[%-10s][%d%%]\r", bar, rate);
  fflush(stdout);
  if(rate == 100) printf("\n");
}

